<?php
/*
	打印机msg
	参数：$sendcontent,$order_id,$is_pay
	订单类型：1堂点，2外卖，3自取，4排队，5预约
	通知类型：0下单通知，1
*/
defined('BY_JYA') or exit('error');
list($sendcontent,$order_id,$is_pay,$daike)=$arguments;
//打印内容：
$order=pdo_get('jy_dish_order',array('id'=>$order_id));
$goods=pdo_getall('jy_dish_order_goods',array('order_id'=>$order_id));
$out=pdo_getall('jy_dish_order_out',array('order_id'=>$order_id));
$out['out_time'] = $out['out_time']?$out['out_time'] :"尽快配送";
if($goods){
	$str = '';
	foreach($goods as $g){
		$g['goods_package']=$g['goods_package']?json_decode($g['goods_package'],true):array();
		if(empty($g['goods_package'])){
			$money = $g['goods_price'] * $g['goods_num'];
			$str .= ''.str_pad($g['goods_title'], '26', ' ', STR_PAD_RIGHT);
			$str .= ''.str_pad($g['goods_num'], '5', ' ', STR_PAD_RIGHT);
			$str .= ''. number_format($money, 2) . "元\n";
		}else{
			$str .= $g['goods_title']."--规格如下：\n";
			foreach($g['goods_package'] as $p){
					$money = $p['price'] * $p['num'];
					$str .= '['.str_pad($p['title'].']', '23', ' ', STR_PAD_RIGHT);
					$str .= ''.str_pad($p['num'], '5', ' ', STR_PAD_RIGHT);
					$str .= ''. number_format($money, 2) . "元\n";
					
			}
		}
	}
}
$goodsinfo = "名称　             数量    单价\n";
$goodsinfo .= $str;
$goodsinfo .= "---------------------------------\n";
$order_type=$this->type__order();
$order_type=$order_type[$order['order_type']];
$selfinfo=pdo_get('jy_dish_order_self',array('order_id'=>$order_id));
$is_pays =array('0'=>'未支付','1'=>'已支付');
$text = "订单下单通知:\n";
if($order['order_type'] == 3){
	$text .="<AM><S2>取餐号\n</S2></AM>";
	$text .="<AM><S3>".$selfinfo['selfnumber']."\n</S3></AM>";
}
if($daike){
	$nickname = '代客下单';
}
$text .='下单时间:'.date('Y-m-d h:i:s',$order['createtime'])."\n";
$text .='订单类型:'.$order_type."\n";
$text .='消费金额:'.$order['price']."\n";
$text .="是否支付:".$is_pays[$order['is_pay']]."\n";
$text .='下单人:'.$nickname."\n";
$text .="【菜品详情】\n";
$text .="---------------------------------\n";
$text .=$goodsinfo."\n";
if($order['order_type'] == 2){
$text .='用户名:'.$out['name']."\n";
$text .='用户手机:'.$out['mobile']."\n";
$text .='配送地址:'.$out['receiver_address']."\n";
$text .='配送时间:'.$out['out_time']."\n";
}
$text .='备注:'.$order['remark']."\n";
$printers=pdo_getall('jy_dish_printer',array('store_id'=>$order['store_id']));
foreach($printers as &$p){
	if($p['print_type']==1 && $is_pay){
		if($p['is_meal'] && $order['order_type']==1){
			$order=pdo_get('jy_dish_order_table',array('order_id'=>$order_id));
			$table=pdo_get('jy_dish_table',array('id'=>$order['table_id']));
			$content = "<AM><S1>".$p['print_top']."\n</S1></AM>";
			$content .=$text;
			$content .="餐桌号：".$table['title']."\n";
			$content .="用餐人数：".$order['user_num']."位\n";
			$content .= "<AM><S1>".$p['print_bottom']."\n</S1></AM>";
			$content .= "<AM><QR_T>QR_CODE</QR_T><QR_S>7</QR_S><QR_D>".$p['qrcode_url']."</QR_D></AM>";
			$params=array('sendcontent'=>$content,'id'=>$p['printerid']);
			$this->plugin('jy_print')->printer($params,'jy_dish');
		}elseif($p['is_delivery'] && $order['order_type']==2){
			$order=pdo_get('jy_dish_order_out',array('order_id'=>$order_id));
			$order['out_time']=$order['out_time'] ? json_decode($order['out_time'],true) : array();
			$order['out_time']['timev'] =$order['out_time']['timev']?$order['out_time']['timev']:'尽快配送';
			$content = "<AM><S1>".$p['print_top']."\n</S1></AM>";
			$content .=$text;
			$content .="配送时间：".$order['out_time']['timev']."\n";
			$content .="收货人：".$order['receiver_name']."\n";
			$content .="联系电话：".$order['receiver_mobile']."\n";
			$content .="收货地址：".$order['receiver_address']."\n";
			$content .= "<AM><S1>".$p['print_bottom']."\n</S1></AM>";
			$content .= "<AM><QR_T>QR_CODE</QR_T><QR_S>7</QR_S><QR_D>".$p['qrcode_url']."</QR_D></AM>";
			$params=array('sendcontent'=>$content,'id'=>$p['printerid']);
			$this->plugin('jy_print')->printer($params,'jy_dish');
		}elseif($order['order_type']==3){
			$content = "<AM><S1>".$p['print_top']."\n</S1></AM>";
			$content .=$text;
			$content .= "<AM><S1>".$p['print_bottom']."\n</S1></AM>";
			$content .= "<AM><QR_T>QR_CODE</QR_T><QR_S>7</QR_S><QR_D>".$p['qrcode_url']."</QR_D></AM>";
			$params=array('sendcontent'=>$content,'id'=>$p['printerid']);
			$this->plugin('jy_print')->printer($params,'jy_dish');
		}elseif($p['is_reservation'] && $order['order_type']==5){
			$content = "<AM><S1>".$p['print_top']."\n</S1></AM>";
			$content .=$text;
			$content .= "<AM><S1>".$p['print_bottom']."\n</S1></AM>";
			$content .= "<AM><QR_T>QR_CODE</QR_T><QR_S>7</QR_S><QR_D>".$p['qrcode_url']."</QR_D></AM>";
			$params=array('sendcontent'=>$content,'id'=>$p['printerid']);
			$this->plugin('jy_print')->printer($params,'jy_dish');
		}
	}elseif($p['print_type'] == 0 || $daike){
		if($p['is_meal'] && $order['order_type']==1){
			$order=pdo_get('jy_dish_order_table',array('order_id'=>$order_id));
			$table=pdo_get('jy_dish_table',array('id'=>$order['table_id']));
			$content = "<AM><S1>".$p['print_top']."\n</S1></AM>";
			$content .=$text;
			$content .="餐桌号：".$table['title']."\n";
			$content .="用餐人数：".$order['user_num']."位\n";
			$content .= "<AM><S1>".$p['print_bottom']."\n</S1></AM>";
			$content .= "<AM><QR_T>QR_CODE</QR_T><QR_S>7</QR_S><QR_D>".$p['qrcode_url']."</QR_D></AM>";
			$params=array('sendcontent'=>$content,'id'=>$p['printerid']);
			$this->plugin('jy_print')->printer($params,'jy_dish');
		}elseif($p['is_delivery'] && $order['order_type']==2){
			$order=pdo_get('jy_dish_order_out',array('order_id'=>$order_id));
			$order['out_time']=$order['out_time'] ? json_decode($order['out_time'],true) : array();
			$order['out_time']['timev'] =$order['out_time']['timev']?$order['out_time']['timev']:'尽快配送';
			$content = "<AM><S1>".$p['print_top']."\n</S1></AM>";
			$content .=$text;
			$content .="配送时间：".$order['out_time']['timev']."\n";
			$content .="收货人：".$order['receiver_name']."\n";
			$content .="联系电话：".$order['receiver_mobile']."\n";
			$content .="收货地址：".$order['receiver_address']."\n";
			$content .= "<AM><S1>".$p['print_bottom']."\n</S1></AM>";
			$content .= "<AM><QR_T>QR_CODE</QR_T><QR_S>7</QR_S><QR_D>".$p['qrcode_url']."</QR_D></AM>";
			$params=array('sendcontent'=>$content,'id'=>$p['printerid']);
			$this->plugin('jy_print')->printer($params,'jy_dish');
		}elseif($order['order_type']==3){
			$content = "<AM><S1>".$p['print_top']."\n</S1></AM>";
			$content .=$text;
			$content .= "<AM><S1>".$p['print_bottom']."\n</S1></AM>";
			$content .= "<AM><QR_T>QR_CODE</QR_T><QR_S>7</QR_S><QR_D>".$p['qrcode_url']."</QR_D></AM>";
			$params=array('sendcontent'=>$content,'id'=>$p['printerid']);
			$this->plugin('jy_print')->printer($params,'jy_dish');
		}elseif($p['is_reservation'] && $order['order_type']==5){
			$content = "<AM><S1>".$p['print_top']."\n</S1></AM>";
			$content .=$text;
			$content .= "<AM><S1>".$p['print_bottom']."\n</S1></AM>";
			$content .= "<AM><QR_T>QR_CODE</QR_T><QR_S>7</QR_S><QR_D>".$p['qrcode_url']."</QR_D></AM>";
			$params=array('sendcontent'=>$content,'id'=>$p['printerid']);
			$this->plugin('jy_print')->printer($params,'jy_dish');
		}
		
	}
	pdo_update('jy_dish_order',array('print_status'=>1),array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
	
}

